package com.rongma.waterdrop.fragment.top;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.rongma.waterdrop.R;
import com.rongma.waterdrop.activity.MainActivity;
import com.rongma.waterdrop.adapter.TimerItemAdapter;
import com.rongma.waterdrop.global.TimerStatus;
import com.rongma.waterdrop.global.WaterDropApplication;
import com.rongma.waterdrop.listener.OnCodeTimeChangeListener;
import com.rongma.waterdrop.manager.bmobmanager.TimerEventManager;
import com.rongma.waterdrop.manager.bmobmanager.UserManager;
import com.rongma.waterdrop.model.TimerEvent;
import com.rongma.waterdrop.model.User;
import com.rongma.waterdrop.ui.TextProgressBar;
import com.rongma.waterdrop.utils.LogUtils;
import com.rongma.waterdrop.utils.TimeUtils;

import java.util.ArrayList;

public class TodayFragment extends Fragment
{
    private TextProgressBar progressBar;
    private ArrayList<TimerEvent> events;
    private TimerItemAdapter timerItemAdapter;
    private ListView mLvTimerEvent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.top_today_layout,null);
        progressBar = (TextProgressBar) view.findViewById(R.id.textpb_main);
        int codeTime = UserManager.getManager(getActivity()).queryUserCodeTime("戎码");
        try {
            progressBar.setText("您的编程时间-> "+ TimeUtils.getTimeByNumber(codeTime));
            progressBar.setProgress(codeTime / 100);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        mLvTimerEvent = (ListView) view.findViewById(R.id.lv_main_timer);
        events = new ArrayList<TimerEvent>();
        //加载数据
        initEventData();
        timerItemAdapter = new TimerItemAdapter(events, getActivity());
        mLvTimerEvent.setAdapter(timerItemAdapter);
        timerItemAdapter.setTimeChangeListener(new OnCodeTimeChangeListener() {
            @Override
            public void onChange() {
                try {
                    progressBar.setText("您的编程时间-> "+ TimeUtils.getTimeByNumber(UserManager.getManager(getActivity()).queryUserCodeTime("戎码")));
                    progressBar.setProgress(UserManager.getManager(getActivity()).queryUserCodeTime("戎码")/ 100);
                    progressBar.invalidate();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        return view;
    }

    private void initEventData() {
        events = TimerEventManager.getManager(getActivity()).queryAllTimer();
    }
}
